<template>
  <div class="main">
    <div class="home">

       <!-- <Consider @change="handerchange" :title='titleText' :text="texts" ></Consider> -->
      <Menu title="首页" :menulist="menulist" v-if="menuHide"></Menu>
      <Content :widths="widths"></Content>
      <Sidebar v-if="sidebarHide">
        <!-- <Tips :icon="Tipsicon" :text="Tipstext" ref="childTips"></Tips> -->
        <template #b>
          <HotSerachList  />
        </template>
        <template #c>
          <div class="recommended-person" v-if="!articleSider">
            <div class="recom-title">
              <p class="left">你可能感兴趣的人</p>
              <div class="right">
                <i class="iconfont icon-zuojiantou" @click.stop="pageLeft"></i><span>{{UserPage}}</span>/<span>{{pageSize}}</span
                ><i class="iconfont icon-youjiantou" @click.stop="pageRight"></i>
              </div>
            </div>
            <div
              class="recom-user"
              v-for="(item,index) in interesteUserPageList"
              :key="item.uid"
            >
              <div class="left" @click="gotouser(item.uid)">
                <div class="userimg">
                  <img :src="item.avatar" alt="" />
                </div>
                <div class="userinfo">
                  <p class="userinfo-name">{{ item.username }}</p>
                  <p class="userinfo-introduced">{{ item.lable }}</p>
                </div>
              </div>
              <div class="right">
                <button class="right-follow" v-if="!item.isfollow"  @click="handerFollow(item.uid ,index)">
                  <i class="iconfont icon-jiahao"></i><span>关注</span>
                </button>
                 <button v-else class="right-followed "   >
                  <i class="iconfont icon-jiahao"></i><span>已关注</span>
                </button>
              </div>
            </div>
          </div>
        </template>
      <!-- 用户详情 -->
        <template #a>
          <div class="home-page" v-if="articleSider">
            <div class="home-page-header">
              <div class="header-background">
                <img :src="bg_url"  alt="" />
              </div>
              <div class="header-userimg" @click="gotouser(userInfo.uid)">
                <img :src="userInfo.avatar" alt="" />
              </div>
            </div>
            <div class="home-page-center">
              <div class="center-username">{{userInfo.username}}</div>
              <div class="center-card">
                <li>
                  <p>{{userInfo.fllow_COUNT?  userInfo.fllow_COUNT :0 }}</p>
                  <span>关注</span>
                </li>
                <li>
                  <p>{{userInfo.fans_str}}</p>
                  <span>粉丝</span>
                </li>
                <li>
                  <p>{{userInfo.article_num}}</p>
                  <span>文章</span>
                </li>
              </div>
              <div class="center-info">
                <div class="con">
                  <i class="iconfont icon-ip"></i><span>{{userInfo.lable}}</span>
                </div>
                <div class="con">
                  <i class="iconfont icon-gerenjianjie"></i
                  ><span
                    >{{userInfo.introduce? userInfo.introduce : '暂无简介'}}</span
                  >
                </div>
                <div class="con">
                  <i class="iconfont icon-rili1"></i>
                  <span v-if="userInfo.gender===1">男</span>
                  <span v-else-if="userInfo.gender===2">女</span>
                  <span v-else-if="userInfo.gender===0">未知</span>
                  <span>{{userInfo.birthday}} </span>
                  <span>{{userInfo.homeLocation}}</span>
                </div>
              </div>
            </div>
            <div class="home-page-bottom" @click="gotouser(userInfo.uid)">
              查看个人主页<i class="iconfont icon-youjiantou"></i>
            </div>
          </div>
        </template>
        <template #p>
          <Footer></Footer>
        </template>
      </Sidebar>
    </div>
  </div>
</template>

<script>
// @ is an alias to /src
import { mapMutations, mapState } from 'vuex'
import http from '@/util/http'
import scroll from '@/util/scroll'
import Menu from '../../components/MenuView.vue'
import Content from '../../components/contentView.vue'
import Sidebar from '../../components/sidebarView.vue'
import HotSerachList from '../../components/hotSerachList.vue'
import Footer from '@/components/footerView.vue'
// import Tips from '@/components/utilView/tipView.vue'

// import axios from 'axios'
export default {
  components: {
    Menu,
    Content,
    Sidebar,
    HotSerachList,
    Footer

  },
  data () {
    return {
      Tipstext: '',
      Tipsicon: '',
      menulist: [
        {
          icon: 'icon-tuijian',
          name: '推荐',
          path: '/home/all'
        },
        {
          icon: 'icon-chongwu1',
          name: '萌宠',
          path: '/home/lactation'
        },
        {
          icon: 'icon-dianlikepu',
          name: '科普',
          path: '/home/bird'
        },
        {
          icon: 'icon-zhongcao',
          name: '种草',
          path: '/home/crawl'
        },
        {
          icon: 'icon-chongwu',
          name: '异宠',
          path: '/home/fish'
        }
      ],
      datalist: [],
      interesteUserList: [], // 可能感兴趣的人总数据
      interesteUserPageList: [], // 可能感兴趣的人分页数据
      UserPage: 1, // 可能感兴趣的用户的页码
      userInfo: {}, // 用户详情
      titleText: '', // 弹出框标题
      texts: '', // 弹出框正文
      screenWidth: document.body.clientWidth, // 窗口当前的可视宽度
      widths: '100%', // 主要内容容器的宽度
      sidebarHide: true, // 侧边栏的隐藏控制
      menuHide: true, // 侧边栏菜单的隐藏控制

      // 用户介绍背景图
      bg_url: '',
      pageSize: 0 // 可能感兴趣的用户的页码
    }
  },

  mounted () {
    // 感兴趣的人的数据
    http.userinterested(this.user.uid).then((res) => {
      this.interesteUserList = res.data.newUser
      this.pageSize = res.data.page_size
      this.interesteUserPageList = this.interesteUserList.slice(0, 3)
    })
    // 文章作者用户详情信息
    // uid 第一个参数登录用户的id  第二个参数uuid是被查看用户的id
    if (this.articleSider) {
      this.getUser()
    }

    // 加载页面数据 获取宽度
    this.getNowWidth()
    // 监听窗口宽度的变化

    const that = this
    window.onresize = () => {
      return (() => {
        window.screenWidth = document.body.clientWidth
        that.screenWidth = window.screenWidth
      })()
    }
    // console.log('显示隐藏侧边栏的变量', this.sidebarHide)
  },
  updated () {

  },

  computed: {
    // 共享状态
    ...mapState(['articleSider', 'user', 'userInfoId'])

  },

  watch: {
    articleSider (newval) {
      if (newval) {
        this.getUser()
      }
    },
    screenWidth (val) {
      // 为了避免频繁触发resize函数导致页面卡顿，使用定时器
      if (!this.timer) {
        // 一旦监听到的screenWidth值改变，就将其重新赋给data里的screenWidth
        this.screenWidth = val
        this.timer = true
        const that = this
        if (this.screenWidth <= 1152 && this.screenWidth > 856) {
          this.sidebarHide = false
          this.menuHide = true
          this.widths = '685px'
        } else if (this.screenWidth > 1152) {
          this.sidebarHide = true
          this.widths = '640px'
          this.menuHide = true
        } else if (this.screenWidth <= 856 && this.screenWidth > 540) {
          this.widths = '8.5rem'
          this.sidebarHide = false
          this.menuHide = true
        } else if (this.screenWidth <= 540) {
          this.widths = '11.22rem'
          this.sidebarHide = false
          this.menuHide = false
        }
        setTimeout(function () {
          // console.log(that.widths)
          // 打印screenWidth变化的值
          console.log(that.screenWidth)
          that.timer = false
        }, 300)
      }
    }

  },

  methods: {
    ...mapMutations(['isLogin', 'updateTabbar', 'updateMenu', 'navbarHide', 'menusHide', 'isArticleSider']),
    // tipsMethod (text, icon) {
    //   this.$refs.childTips.HanderTiming(1200)
    //   this.Tipstext = text
    //   this.Tipsicon = icon
    // },
    // 获取当前屏幕的高度
    // 获取用户详情
    getUser () {
      http.userinfo(this.user.uid, this.userInfoId).then((res) => {
        console.log(res.data.userdata)
        this.userInfo = res.data.userdata.user
        this.bg_url = res.data.userdata.user.user_bg_img.bg_url
        // this.userInfo.user_bg_img.bg_url
        // 计算图片位置
      })
    },
    async getNowWidth () {
      this.screenWidth = await scroll.getClientWidth()
      if (this.screenWidth < 1152) {
        this.sidebarHide = false
      } else if (this.screenWidth > 1152) {
        this.sidebarHide = true
      }
      if (this.screenWidth <= 540) {
        // 给主页二级菜单触底内容
        this.updateMenu(this.menulist)
        this.menuHide = false
        // 隐藏pc顶部导航栏
        this.isLogin(false)
        // 显示底部的导航栏
        this.updateTabbar(true)
        // 显示手机端头部导航栏
        this.navbarHide(true)
        // 显示手机端头部菜单menumini
        this.menusHide(true)
      } else if (this.screenWidth > 540) {
        // 显示pc顶部导航栏
        this.isLogin(true)
        // 隐藏底部的导航栏
        this.updateTabbar(false)
        // 隐藏手机端头部导航栏
        this.navbarHide(false)
      }
    },

    // 可能感兴趣的人翻页
    pageLeft () {
      console.log('左')
      const list = this.interesteUserList

      if (this.UserPage > 1) {
        this.UserPage--
        this.interesteUserPageList = list.slice((this.UserPage - 1) * 3, this.UserPage * 3)
      }
    },
    pageRight () {
      console.log('右')
      const list = this.interesteUserList
      if (this.UserPage < this.interesteUserList.length / 3) {
        this.UserPage++
        this.interesteUserPageList = list.slice((this.UserPage - 1) * 3, this.UserPage * 3)
      }
    },
    // 关注
    handerFollow (id, index) {
      // 校验登录
      const checkState = this.$checkU()
      if (!checkState) {
        // this.tipsMethod('请先登录', 'icon-jinggao')
        return
      }
      // this.user.uid 登录的用户id  id 待关注的用户id
      http.getfollow(this.user.uid, id, 1).then(res => {
        console.log(res.data)
        if (res.data.ok === 1) {
          this.interesteUserPageList[index].isfollow = true
        }
      })
    },
    // 用户详情板块跳转到用户详情页面
    gotouser (uid) {
      this.$router.push({ path: '/home/userHome', query: { uuid: uid } })
    }

  }
}
</script>
<style lang="scss" scoped>
 .main{
    width: 1122px;
    margin: 0 auto;
    height: auto;
    padding-top: 64px;

  }
@media screen and (min-width:856px) and (max-width:1154px) {
    .main{
    width: 830px;
    margin: 0 auto;
    height: auto;
    padding-top: 64px;

  }
}
@media screen and (min-width:540px) and (max-width:856px) {
    .main{
    width: 11.22rem;
    margin: 0 auto;
    height: auto;
    padding-top: 64px;

  }
}
@media screen  and (max-width:540px) {
    .main{
    width: 11.22rem;
    margin: 0 auto;
    height: auto;
    padding-top: 0px;

  }
}
.home {
  display: flex;
  justify-content: flex-start;
  height: auto;

  // 广告
  .advertise-box{
    width: 100%;
    position: relative;
    .ad-bg{
      width: 100%;
      .inside-bg{
        width: 100%;
         img {
              display: block;
              width: 100%;
              object-fit: cover;
              border-radius: 6px;
            }
      }
      .ad {
          position: absolute;
          top: 0px;
          right: 0px;
          cursor: pointer;
          display: flex;
          align-items: center;
          span {
            font-size: 12px;
            color: #333;
            background: #f5f5f5;
            opacity: 0.4;
            padding: 3px;
            text-align: center;
            border-radius: 0 6px 0 6px;

            i {
              font-size: 12px;
              color: #333;
              padding-left: 3px;
            }
          }
        }
    }

  }

  .recommended-person {
    width: 100%;
    min-height: 250px;
    background: #fff;
    margin-top: 10px;
    border-radius: 5px;
    .recom-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px;
      border-bottom: 1px #eee solid;
      cursor: pointer;
      .left {
        font-size: 14px;
        padding-left: 10px;
        color: #333;
      }
      .right {
        font-size: 12px;

        i {
          font-size: 12px;
          padding: 7px 7px;
          margin: 7px 7px;
          &:hover {
            color: #f60;
            background: #fff3ed;
            border-radius: 50%;
          }
        }
      }
    }
    .recom-user {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 10px;
      border-bottom: 1px solid rgb(247, 247, 247);
      cursor: pointer;
      &:hover {
        background: #eee;
      }
      .left {
        display: flex;
        align-items: center;
        .userimg {
          width: 50px;
          height: 50px;
          border-radius: 50%;
          background: #eee;

          img {
            display: block;
            width: 100%;
            object-fit: cover;
            border-radius: 50%;
          }
        }
        .userinfo {
          font-size: 12px;
          color: #333;
          padding-left: 10px;
          line-height: 16px;
          .userinfo-introduced {
            width: 130px;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
            color: #999;
          }
        }
      }
      .right {
        display: flex;
        align-items: center;
        .right-follow{
          font-size: 12px;
          display: block;
          width: 65px;
          height: 28px;

          border-radius: 15px;
          border: 1px solid #f60;
          color: #f60;
          background: #fff;
          margin-top: 10px;
          cursor: pointer;
          &:hover {
                background: rgba($color: #f60, $alpha: 0.3);
            color: white;

          }
        }
        .right-followed {
          font-size: 12px;
          display: block;
          width: 65px;
          height: 28px;

          border-radius: 15px;
          border: 1px solid #000;
          color: #000;
          background: #fff;
          margin-top: 10px;
          cursor: pointer;
          &:hover {
                background: rgba($color: #999, $alpha: 0.3);
          }
        }
      }
    }
  }

  .home-page {
    width: 100%;
    border-radius: 3px;
    overflow: hidden;
    .home-page-header {
      width: 100%;
      position: relative;
      // min-height: 80px;
      // background: #fff;
      .header-background {
        width: 100%;
        height: 100px;
        overflow: hidden;
        background: #fff;
        img {
          height: 100%;
          width: 100%;
          object-fit: cover;
        }
      }
      .header-userimg {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        background: #eee;
        position: absolute;
        top: 20%;
        left: 42%;
        cursor: pointer;

        img {
          width: 100%;
          border-radius: 50%;
          object-fit: cover;
          &:hover{
            filter: brightness(70%);
          }
        }
      }
    }
    .home-page-center {
      width: 100%;
      padding: 15px 0 0 0;
      .center-username {
        text-align: center;
        font-size: 14px;
      }
      .center-card {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 15px 30px;
        text-align: center;
        font-size: 14px;
        color: #333;
        span {
          font-size: 12px;
          color: #ccc;
        }
      }
      .center-info {
        font-size: 12px;
        color: #333;
        background: #fff;
        line-height: 18px;
        .con {
          border-bottom: 1px solid rgb(249, 249, 249);
          padding: 15px;
          display: flex;
          i {
            padding-right: 5px;
            color: #ccc;
            font-size: 18px;
            // width: 20px;
            // height: 18px;
          }
          span {
            padding: 0 5px;
          }
        }
      }
    }
    .home-page-bottom {
      font-size: 14px;
      background: #fff;
      text-align: center;
      padding: 15px;
      margin: 10px 0;
      cursor: pointer;
      &:hover{
        color: #f60;
      }
    }
  }
}
</style>
